package com.leetcode.August;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 3. 无重复字符的最长子串
 *  滑动窗口
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-16 23:39
 */
public class LC3无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        int l=0;
        Set<Character> set = new HashSet<>();
        char[] ch=s.toCharArray();
        int res=0;
        for (int i = 0; i < ch.length; i++) {
            char c=ch[i];
            while (set.contains(c)){
                set.remove(ch[l++]);
            }
            set.add(c);
            res=Math.max(res,i-l+1);
        }
        return res;
    }
}
